//
//  UIBarButtonItem+Category.m
//  Beautiful Travel
//
//  Created by Apple on 16/1/21.
//  Copyright © 2016年 Apple. All rights reserved.
//
#import <objc/runtime.h>
#import "UIBarButtonItem+Category.h"
static const char c = '\0';
@implementation UIBarButtonItem (Category)
+ (instancetype) itemWithImage:(UIImage *)image style:(UIBarButtonItemStyle)style action:(UIBarButtonItemBlock) block
{

    UIBarButtonItem * item = [[UIBarButtonItem alloc] initWithImage:image style:style target:self action:@selector(lonnie_action:)];
    objc_setAssociatedObject(item, &c, block, OBJC_ASSOCIATION_RETAIN);
    return item;
}

+(instancetype) itemWithImageName:(NSString *)imageName style:(UIBarButtonItemStyle)style action:(UIBarButtonItemBlock)block
{
    return [self itemWithImage:[UIImage imageNamed:imageName] style:style action:block];
}

+ (void) lonnie_action:(UIBarButtonItem*) item
{
  UIBarButtonItemBlock block =  objc_getAssociatedObject(item, &c);
    if (block) {
        block(item);
    }
}

+ (instancetype) itemWithTitle:(NSString *)title style:(UIBarButtonItemStyle)style action:(UIBarButtonItemBlock)block
{
    UIBarButtonItem * item = [[UIBarButtonItem alloc]  initWithTitle:title style:style target:self action:@selector(lonnie_action:)];
    objc_setAssociatedObject(item, &c, block, OBJC_ASSOCIATION_RETAIN);
    return item;
}
+ (instancetype) itemWithView:(UIView *) view action :(UIBarButtonItemBlock)block
{
    UIBarButtonItem * item = [[UIBarButtonItem alloc] initWithCustomView:view];
    objc_setAssociatedObject(item, &c, block, OBJC_ASSOCIATION_RETAIN);
    return item;
}
@end
